package com.controller
{
	
	
	
	import com.events.WarehouseMasterEvent;
	import com.model.valueObjects.VOWarehouseMaster;
	import com.service.WarehouseMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class WarehouseMasterController extends AbstractController
	{
		import com.model.Model;
		
		[Inject]
		public var warehouseMasterDetails:WarehouseMaster;
		//[PostConstruct]
		public function WarehouseMasterController()
		{
			
		}
		
		[EventHandler(event="WarehouseMasterEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(warehouseMasterDetails.getAllWarehouses(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.warehouseMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn = true;
		}
		
		[EventHandler(event="WarehouseMasterEvent.ADD", properties="warehouseMastervo")]
		public function add(warehouseMastervo:VOWarehouseMaster):void
		{
			sh.executeServiceCall(warehouseMasterDetails.addNewWarehouse(warehouseMastervo), addResult, handleFault );	
		}
		private function addResult(event:ResultEvent):void
		{			
			model.warehouseMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Warehouse Added" );
		}
		
		[EventHandler(event="WarehouseMasterEvent.UPDATE", properties="warehouseMastervo")]
		public function updateVal(warehouseMastervo:VOWarehouseMaster):void
		{
			sh.executeServiceCall(warehouseMasterDetails.editWarehouse(warehouseMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.warehouseMasterList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ");
		}
		
		[EventHandler(event="WarehouseMasterEvent.DELETE", properties="serialnumber")]
		public function deleteR(serialnumber:Number):void
		{
			sh.executeServiceCall(warehouseMasterDetails.deleteWarehouse(serialnumber), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			getAll()
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Record Deleted " );
		}
		
		[EventHandler(event="WarehouseMasterEvent.GET_BRANCH")]
		public function getBranch():void{
			sh.executeServiceCall(warehouseMasterDetails.getBranch(),getBranches, handleFault );
		}
		private function getBranches(event:ResultEvent):void
		{
			model.branchList=event.result as ArrayCollection;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}